package com.niodata.dp.core.usermgr.dao;

import com.niodata.dp.core.usermgr.enums.GroupRole;
import com.niodata.dp.core.usermgr.enums.SystemRole;
import com.niodata.dp.core.usermgr.model.UserInfo;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;

/**
 * Created by ndp on 17-3-9.
 */
@Mapper
public interface UserInfoMapper {

  @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "USER_ID")
  void insert(@Param("userInfo") UserInfo userInfo);

  int updateInfo(@Param("userId") Long userId, @Param("email") String email,
        @Param("password") String password, @Param("detail") String detail);

  int updateRole(@Param("userId") Long userId, @Param("role") SystemRole role);

  int delete(@Param("userId") Long userId);

  UserInfo getUserInfo(@Param("userId") Long userId);

  List<UserInfo> getUserInfoListBySystemRole(@Param("role") SystemRole role,
        @Param("offset") int offset, @Param("size") int size);

  Integer getUserInfoCountBySystemRole(@Param("role") SystemRole role);

  List<UserInfo> getUserInfoList(@Param("userIds") Long... userIds);

  UserInfo checkPassword(@Param("loginNameOrEmail") String loginNameOrEmail,
        @Param("password") String password);

  UserInfo getUserInfoByLogInName(@Param("loginName") String loginName);

  UserInfo getGroupOwner(@Param("groupId") Long groupId);

  List<UserInfo> getGroupUsers(@Param("groupId") Long groupId, String role);

  /**
   * 获取组内非OWNER成员，包括全部的MASTER、MEMBER.
   *
   * @param groupId 组ID
   * @return 对应的用户信息
   */
  List<UserInfo> getGroupMembers(@Param("groupId") Long groupId);

  /**
   * 根据组ID和组的角色类型获取该组下面的全部用户信息.
   *
   * @param groupId 组ID
   * @param role 组角色类型
   * @return 对应的用户信息
   */
  List<UserInfo> getByGroupRoleAndGroupId(@Param("groupId") Long groupId,
        @Param("role") GroupRole role);

  @Select("select * from USER_INFO WHERE USER_NAME LIKE CONCAT('%',#{name},'%')")
  @ResultMap("userInfoResult")
  List<UserInfo> searchUserByName(@Param("name") String name);


}
